home *** CD-ROM | disk | FTP | other *** search
/ Turnbull China Bikeride / Turnbull China Bikeride - Disc 2.iso / STUTTGART / LANG / SCHEME / GNU / SCM4E1 / !Scm / slib / synchk < prev    next >
Text File  |  1993-02-18  |  4KB  |  106 lines

  1. ;;; -*-Scheme-*-
  2. ;;;
  3. ;;; Copyright (c) 1989-91 Massachusetts Institute of Technology
  4. ;;;
  5. ;;; This material was developed by the Scheme project at the
  6. ;;; Massachusetts Institute of Technology, Department of Electrical
  7. ;;; Engineering and Computer Science.  Permission to copy this
  8. ;;; software, to redistribute it, and to use it for any purpose is
  9. ;;; granted, subject to the following restrictions and understandings.
  10. ;;;
  11. ;;; 1. Any copy made of this software must include this copyright
  12. ;;; notice in full.
  13. ;;;
  14. ;;; 2. Users of this software agree to make their best efforts (a) to
  15. ;;; return to the MIT Scheme project any improvements or extensions
  16. ;;; that they make, so that these may be included in future releases;
  17. ;;; and (b) to inform MIT of noteworthy uses of this software.
  18. ;;;
  19. ;;; 3. All materials developed as a consequence of the use of this
  20. ;;; software shall duly acknowledge such use, in accordance with the
  21. ;;; usual standards of acknowledging credit in academic research.
  22. ;;;
  23. ;;; 4. MIT has made no warrantee or representation that the operation
  24. ;;; of this software will be error-free, and MIT is under no
  25. ;;; obligation to provide any services, by way of maintenance, update,
  26. ;;; or otherwise.
  27. ;;;
  28. ;;; 5. In conjunction with products arising from the use of this
  29. ;;; material, there shall be no use of the name of the Massachusetts
  30. ;;; Institute of Technology nor of any adaptation thereof in any
  31. ;;; advertising, promotional, or sales literature without prior
  32. ;;; written consent from MIT in each case.
  33.  
  34. ;;;; Syntax Checking
  35. ;;; written by Alan Bawden
  36. ;;; modified by Chris Hanson
  37.  
  38. (define (syntax-check pattern form)
  39.   (if (not (syntax-match? (cdr pattern) (cdr form)))
  40.       (syntax-error "ill-formed special form" form)))
  41.  
  42. (define (ill-formed-syntax form)
  43.   (syntax-error "ill-formed special form" form))
  44.  
  45. (define (syntax-match? pattern object)
  46.   (let ((match-error
  47.      (lambda ()
  48.        (impl-error "ill-formed pattern" pattern))))
  49.     (cond ((symbol? pattern)
  50.        (case pattern
  51.          ((IDENTIFIER) (identifier? object))
  52.          ((DATUM EXPRESSION FORM) #t)
  53.          ((R4RS-BVL)
  54.           (let loop ((seen '()) (object object))
  55.         (or (null? object)
  56.             (if (identifier? object)
  57.             (not (memq object seen))
  58.             (and (pair? object)
  59.                  (identifier? (car object))
  60.                  (not (memq (car object) seen))
  61.                  (loop (cons (car object) seen) (cdr object)))))))
  62.          ((MIT-BVL) (lambda-list? object))
  63.          (else (match-error))))
  64.       ((pair? pattern)
  65.        (case (car pattern)
  66.          ((*)
  67.           (if (pair? (cdr pattern))
  68.           (let ((head (cadr pattern))
  69.             (tail (cddr pattern)))
  70.             (let loop ((object object))
  71.               (or (and (pair? object)
  72.                    (syntax-match? head (car object))
  73.                    (loop (cdr object)))
  74.               (syntax-match? tail object))))
  75.           (match-error)))
  76.          ((+)
  77.           (if (pair? (cdr pattern))
  78.           (let ((head (cadr pattern))
  79.             (tail (cddr pattern)))
  80.             (and (pair? object)
  81.              (syntax-match? head (car object))
  82.              (let loop ((object (cdr object)))
  83.                (or (and (pair? object)
  84.                     (syntax-match? head (car object))
  85.                     (loop (cdr object)))
  86.                    (syntax-match? tail object)))))
  87.           (match-error)))
  88.          ((?)
  89.           (if (pair? (cdr pattern))
  90.           (or (and (pair? object)
  91.                (syntax-match? (cadr pattern) (car object))
  92.                (syntax-match? (cddr pattern) (cdr object)))
  93.               (syntax-match? (cddr pattern) object))
  94.           (match-error)))
  95.          ((QUOTE)
  96.           (if (and (pair? (cdr pattern))
  97.                (null? (cddr pattern)))
  98.           (eqv? (cadr pattern) object)
  99.           (match-error)))
  100.          (else
  101.           (and (pair? object)
  102.            (syntax-match? (car pattern) (car object))
  103.            (syntax-match? (cdr pattern) (cdr object))))))
  104.       (else
  105.        (eqv? pattern object)))))
  106.